<?php
class ManageUser extends Controller {
	function ManageUser(){
		parent::Controller();
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		$this->load->model('Sysadminmodel','',TRUE);
		
		$this->load->database();	
                session_start();

	}
	var $limit =20;
	function index() {
		if(isset($_SESSION['sys_admin']) and isset($_SESSION['name'])) {
			$this->formManage();
		} else {
			
			$this->load->view('layout/head', $this->data);
			$this->load->view('layout/body', $this->data);
			$this->load->view('layout/login-index', $this->data);
			$this->load->view('layout/footer', $this->data);
		}
	}
	function formManage($offset = 0){
		$this->data['title']='Manage User';
		$this->data['h2_title'] ='Manage User';
		$this->data['main_view'] ='sysadmin/manageUsers';
		
		//offset
		$uri_segment = 4;
		$offset = $this->uri->segment($uri_segment);
		$this->data['manage'] = $this->Sysadminmodel->show_all_manage($offset);
		// load data table berita_utama
		$manage_user = $this->Sysadminmodel->show_all_manage($offset)->result();
		$num_rows = $this->Sysadminmodel->count_all_num_rows();
		
		if($num_rows > 0) { // jika query menghasilkan data
			// membuat pagination
			$config['base_url'] = site_url('sysadmin/manageUser/formManage');
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->limit;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$this->data['pagination'] = $this->pagination->create_links();
			
			//set template table untuk efek selang-sling tiap baris
			$tmpl = array('table_open' =>'<table border="0.1" cellpadding="0" cellspacing="0" width="880">','row_alt_start' =>'<tr class="tb-jd">','row_alt_end' =>'</tr>');
			$this->table->set_template($tmpl);
			
			//set heading untul tabel
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('No','User Name','Email','Dept Name','Mandiri Id','Status','Actions');
			
			//Penomeran baris data
			$this->data['i'] = 0 + $offset;
			$i = 0 + $offset;
			foreach($manage_user as $manage) {
								
				//penyusunan data baris perbaris
				$this->table->add_row(++$i,$manage->name,$manage->email,$manage->dept_name,$manage->mandiri_id,$manage->status,
				anchor('sysadmin/manageUser/update/'.$manage->user_id,'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ',array('class'=>'update')).''.
				anchor('sysadmin/manageUser/delete/'.$manage->user_id,'delete ',array('class'=>'delete2','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')")));
				
			}
			$this->data['table'] = $this->table->generate();
			
			
		}
		else {
			$this->data['message'] = 'tidak di temukan satupun data User';
		}
		$this->data['link'] = array('link_add'=>anchor('sysadmin/sysAdmin/formReg','tambah user',array('class' =>'add')));
		
		$this->load->vars($this->data);
		$this->load->view('layout/head');
		$this->load->view('layout/body');
		$this->load->view('layout/slice-atas');
		$this->load->view('sysadmin/manageUsers');
		$this->load->view('layout/slice_bawah');
	
		
	
	}
	function delete($id) {
		$this->Sysadminmodel->delete_user($id);
		$this->session->set_flashdata('message','1 data user berhasil di hapus');
		
		redirect('sysadmin/manageUser');
	}
	function update($id) {
	
		//inisialisasi data umum
		
		$this->data['title']='Update User';
		$this->data['h2_title'] = '';

		$this->data['form_action'] = site_url('sysadmin/manageUser/update_process');
		$this->data['link'] = array('link_back' =>anchor('sysadmin/manageUser/','kembali',array('class' => 'back')));
		
		//cari data dari database
		$user = $this->Sysadminmodel->get_user_by_id($id)->row();
		
		$this->session->set_userdata('user_id', $user->user_id);
		
		//data untuk mengisi field2 form
		$this->data['default']['user_id'] = $user->user_id;
		$this->data['default']['name'] = $user->name;
		$this->data['default']['fname'] = $user->fname;
		$this->data['default']['email'] =$user->email;
		$this->data['default']['pass'] =$user->pass;
		$this->data['default']['pass2']=$user->pass2;
		$this->data['default']['dept_id']=$user->dept_id;
		$this->data['default']['mandiri_id']=$user->mandiri_id;
		$this->data['default']['status']=$user->status;
		$this->data['default']['user_right']=$user->user_right;
		
		//buat session untuk menyimpan data tanggal yang sedang di update
		$this->load->vars($this->data);
		$this->load->view('layout/head');
		$this->load->view('layout/body');
		$this->load->view('layout/slice-atas');
		$this->load->view('sysadmin/updateUser');
		$this->load->view('layout/slice_bawah');
		
		
		
		
	}
	function update_process() {
			//simpan data
		if(isset($_POST['kirim'])) {	
			$id= $this->input->post('user_id');
			$users = array(
						'name' =>strip_tags($this->input->post('name')),
						'fname' =>strip_tags($this->input->post('fname')),
						'email' =>$this->input->post('email'),
						'pass' =>md5($this->input->post('pass')),
						'pass2' =>$this->input->post('pass2'),
						'dept_id' =>$this->input->post('dept_id'),
						'mandiri_id' =>$this->input->post('mandiri_id'),
						'user_right' =>$this->input->post('user_right'));
						//'status' =>$this->input->post('status')
			//$this->db->where('user_id',$id);
			//$this->db->update('user');
			//$this->db->set('name',$this->input->post('name'));
			$this->Sysadminmodel->update($this->session->userdata('user_id'), $users);
			// set pesan
			$this->session->set_flashdata('message','satu User berhasil di update',$this->input->post('name'));
			
		
		redirect('sysadmin/manageUser');
		}
	}
	function callback() {
		
	}
}
?>